<?php
//admin for images

class Controller extends controllerBase {

	public function index()
	{
		$title = 'images';
		$content = 'this is the admin section for images';
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function add()
	{
		global $headers;
		
		$errors = array();
		$image = new image();
		if(isset($_POST['submit']))
		{
			
			$image->url = $_FILES["imgFile"]["name"];
			$image->title = $_POST['title'];
			$image->caption = $_POST['caption'];
			$image->uploadDate = time();
			
			$date = split('/', $_POST['date']);
			$image->date = mktime(0, 0, 0, $date[0], $date[1], $date[2]);
			
			if(self::upload($_FILES["imgFile"]))
			{
				$image->save();
				$submitted = true;
			}
			else
			{
				$errors[] = 'upload-fail';
			}
		}
		
		if($submitted)
		{
			$content = "yay upload";
		}
		else
		{
			$content = form::getForm('image_add', array($errors));
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function edit($id = 0)
	{
		global $headers;
		
		if($id > 0)
		{
			$errors = array();
			$image = new image($id);
			
			if(isset($_POST['submit']))
			{
				$image->title = $_POST['title'];
				$image->caption = $_POST['caption'];
				$date = split('/', $_POST['date']);
				print_r($date);
				$image->date = mktime(0, 0, 0, $date[0], $date[1], $date[2]);
				$image->save();
				
				$edit = true;
			}
			
			if($edit)
			{
				$content = 'image edited';
			}
			else
			{
				$content = form::getForm('image_edit', array($errors, $image));
			}
		}
		else
		{
			$query = new Query("SELECT * FROM `images` ORDER BY `id`");
			
			if($query->num_rows() > 0)
			{
				$imgs = image::RecordSet($query, 'image');
				
				$show .= '<table>';
				
				foreach($imgs as $img)
				{
					$show .= '<tr><td><img src="' . ROOT_URL . 'content/images/small/' . $img->url . '" alt="' . $img->title . '" /></td>
							   <td><a href="edit/' . $img->id . '">Edit</a> || <a href="delete/' . $img->id . '">Delete</a></td></tr>';
				}
				$show .= '</table>';
				
				$content = $show;
			}
			else
			{
				$content = 'There are no images.';
			}
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function delete($id)
	{
		$image = new image($id);
		
		unlink(ROOT . 'content/images/' . $image->url);
		unlink(ROOT . 'content/images/small/' . $image->url);
		unlink(ROOT . 'content/images/med/' . $image->url);
		$image->delete();
		$content = 'image deleted';
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function upload5()
	{
		$errors = array();
		
		if(isset($_POST['submit']))
		{
			$i = 1;
			foreach ($_FILES["imgFile"]["error"] as $key => $error)
			{
					if ($error == UPLOAD_ERR_OK)
					{
						$ximage = new image();
						$ximage->date = $ximage->uploadDate = time();
						$ximage->url = $_FILES["imgFile"]["name"][$key];
						
						include(MODULE_DIR . 'image/config.php');
		
						#actually upload image!
						$uploadpath = ROOT . "content/images/";
						$uploadpath = $uploadpath.basename($_FILES["imgFile"]["name"][$key]);
						if (!move_uploaded_file($_FILES["imgFile"]["tmp_name"][$key], $uploadpath))
							return false;
						$image_name = ROOT . "content/images/".$_FILES["imgFile"]["name"][$key];

						//make thumbnail
						list($width,$height) = getimagesize($image_name);
						$new_image_name = ROOT . "content/images/small/".$_FILES["imgFile"]["name"][$key];
						if ($width > $height)
						{
						$ratio = ($config['modules']['images']['small-image-max-dim']/$width);
						$new_width = round($width*$ratio);
						$new_height = round($height*$ratio);
						}
						else
						{
						$ratio = ($config['modules']['images']['small-image-max-dim']/$height);
						$new_width = round($width*$ratio);
						$new_height = round($height*$ratio);
						}
						$image_p = imagecreatetruecolor($new_width,$new_height);
						$image = imagecreatefromjpeg($image_name);
						imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
						imagejpeg($image_p,$new_image_name,100);


						//make display image

						list($width,$height) = getimagesize($image_name);
						$new_image2_name = ROOT . "content/images/med/".$_FILES["imgFile"]["name"][$key];
						if ($width > $height)
						{
						$ratio = ($config['modules']['images']['med-image-max-dim']/$width);
						$new_width2 = round($width*$ratio);
						$new_height2 = round($height*$ratio);
						}
						else
						{
						$ratio = ($config['modules']['images']['med-image-max-dim']/$height);
						$new_width2 = round($width*$ratio);
						$new_height2 = round($height*$ratio);
						}
						$image_q = imagecreatetruecolor($new_width2,$new_height2);
						$image = imagecreatefromjpeg($image_name);
						imagecopyresampled($image_q,$image,0,0,0,0,$new_width2,$new_height2,$width,$height);
						imagejpeg($image_q,$new_image2_name,100);
		
						
						$ximage->save();
						
					}
			}
			
			$upload = true;
		}
		
		if($upload)
		{
			$content = 'images uploaded';
		}
		else
		{
			$content = form::getForm('image_upload5', array($errors));
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function upload($imgFile)
	{
		include(MODULE_DIR . 'image/config.php');
		
		#actually upload image!
		$uploadpath = ROOT . "content/images/";
		$uploadpath = $uploadpath.basename($imgFile["name"]);
		if (!move_uploaded_file($imgFile["tmp_name"], $uploadpath))
			return false;
		$image_name = ROOT . "content/images/".$imgFile["name"];

		//make thumbnail
		list($width,$height) = getimagesize($image_name);
		$new_image_name = ROOT . "content/images/small/".$imgFile["name"];
		if ($width > $height)
		{
		$ratio = ($config['modules']['images']['small-image-max-dim']/$width);
		$new_width = round($width*$ratio);
		$new_height = round($height*$ratio);
		}
		else
		{
		$ratio = ($config['modules']['images']['small-image-max-dim']/$height);
		$new_width = round($width*$ratio);
		$new_height = round($height*$ratio);
		}
		$image_p = imagecreatetruecolor($new_width,$new_height);
		$image = imagecreatefromjpeg($image_name);
		imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
		imagejpeg($image_p,$new_image_name,100);


		//make display image

		list($width,$height) = getimagesize($image_name);
		$new_image2_name = ROOT . "content/images/med/".$imgFile["name"];
		if ($width > $height)
		{
		$ratio = ($config['modules']['images']['med-image-max-dim']/$width);
		$new_width2 = round($width*$ratio);
		$new_height2 = round($height*$ratio);
		}
		else
		{
		$ratio = ($config['modules']['images']['med-image-max-dim']/$height);
		$new_width2 = round($width*$ratio);
		$new_height2 = round($height*$ratio);
		}
		$image_q = imagecreatetruecolor($new_width2,$new_height2);
		$image = imagecreatefromjpeg($image_name);
		imagecopyresampled($image_q,$image,0,0,0,0,$new_width2,$new_height2,$width,$height);
		imagejpeg($image_q,$new_image2_name,100);
		return true;
	}
}
?>